package com.queue;

/**
 * @program: letcode
 * @author: lin
 * @create: 2020-03-17 18:01
 * @description: 使用链表的方式实现队列
 * 链表的操作可以结合这个理解： https://mp.weixin.qq.com/s/hKjkITbCRcnZBafpjiwWJA
 *
 */
public class QueueBasedOnLinkedList {

    private class Node{
        private String data;
        private Node next;

        public Node(String data,Node next){
            this.data = data;
            this.next = next;
        }

        public String getData(){
            return this.data;
        }
    }

    private Node head = null;

    private Node tail = null;

    // 进队
    public void enqueue(String value){
        // 判断是否为空队列
        if(tail == null){
            Node newNode = new Node(value,null);
            head = newNode;
            tail = newNode;
        } else {
            // 设置指针域
            tail.next = new Node(value,null);
            // 移动节点指针指向新插入的节点
            tail = tail.next;
            System.out.println("tail:"+tail.data);
        }
    }

    // 出队
    public String dequeue(){
        if(head == null){
            return null;
        }
        String value = head.data;
        head = head.next;
        // 判断当前队列的数据是不是最后一位
        if(head == null){
            tail = null;
        }
        return value;
    }

    public void printAll() {
        Node p = head;
        while (p != null) {
            System.out.print(p.data + " ");
            p = p.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        QueueBasedOnLinkedList a = new QueueBasedOnLinkedList();
        a.enqueue("sa");
        a.enqueue("11");
        System.out.println(a.dequeue());
    }

}
